using System.ComponentModel.DataAnnotations;

namespace Matrix.Configuration.Options
{
    /// <summary>
    /// 缓存配置选项
    /// </summary>
    public class CacheOptions
    {
        /// <summary>
        /// Redis连接字符串
        /// </summary>
        [Required]
        public string RedisConnectionString { get; set; } = string.Empty;

        /// <summary>
        /// 数据库索引
        /// </summary>
        [Range(0, 15, ErrorMessage = "DatabaseIndex must be between 0 and 15")]
        public int DatabaseIndex { get; set; } = 0;

        /// <summary>
        /// 默认过期时间（分钟）
        /// </summary>
        [Range(1, 1440, ErrorMessage = "DefaultExpiration must be between 1 and 1440")]
        public int DefaultExpirationMinutes { get; set; } = 30;

        /// <summary>
        /// 键前缀
        /// </summary>
        public string KeyPrefix { get; set; } = "matrix:";

        /// <summary>
        /// 是否启用本地缓存
        /// </summary>
        public bool EnableLocalCache { get; set; } = true;

        /// <summary>
        /// 本地缓存过期时间（分钟）
        /// </summary>
        [Range(1, 60, ErrorMessage = "LocalCacheExpiration must be between 1 and 60")]
        public int LocalCacheExpirationMinutes { get; set; } = 5;

        /// <summary>
        /// 最大本地缓存条目数
        /// </summary>
        [Range(100, 10000, ErrorMessage = "MaxLocalCacheItems must be between 100 and 10000")]
        public int MaxLocalCacheItems { get; set; } = 1000;

        /// <summary>
        /// 连接超时时间（秒）
        /// </summary>
        [Range(1, 30, ErrorMessage = "ConnectTimeout must be between 1 and 30")]
        public int ConnectTimeout { get; set; } = 10;

        /// <summary>
        /// 同步超时时间（秒）
        /// </summary>
        [Range(1, 30, ErrorMessage = "SyncTimeout must be between 1 and 30")]
        public int SyncTimeout { get; set; } = 10;

        /// <summary>
        /// 验证配置
        /// </summary>
        public void Validate()
        {
            if (string.IsNullOrWhiteSpace(RedisConnectionString))
                throw new ArgumentException("RedisConnectionString is required");

            if (DatabaseIndex < 0 || DatabaseIndex > 15)
                throw new ArgumentException("DatabaseIndex must be between 0 and 15");

            if (DefaultExpirationMinutes <= 0)
                throw new ArgumentException("DefaultExpirationMinutes must be greater than 0");

            if (LocalCacheExpirationMinutes <= 0)
                throw new ArgumentException("LocalCacheExpirationMinutes must be greater than 0");

            if (MaxLocalCacheItems <= 0)
                throw new ArgumentException("MaxLocalCacheItems must be greater than 0");
        }
    }
}